package hutech.ksv.dao;

import hutech.ksv.entity.ThongTinCongTyEntity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DuyetThongTinTuyenDungDao implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	DataSource dataSource;
	
	public List<ThongTinCongTyEntity> getTatCaThongTinTuyenDung() {
		String query = "SELECT * FROM `taikhoanct` as tkct "
				+ "INNER JOIN `congtytuyendung` AS ct ON (`ct`.`MaCTTD` = `tkct`.`MaCTTD`) "
				+ "INNER JOIN `thongtintuyendung` AS tt ON (`tt`.`TenTKCT` = `tkct`.`TenTKCT`) "
				+ "WHERE `tt`.`TrangThaiDuyetTTTD` = 0";
		List<ThongTinCongTyEntity> listThongTinCongTy = new ArrayList<ThongTinCongTyEntity>();

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query);
			ResultSet rs = preparedStatement.executeQuery();
			while (rs.next()) {
				ThongTinCongTyEntity thongTinCongTy = new ThongTinCongTyEntity();
				thongTinCongTy.setTenTKCT(rs.getString("TenTKCT"));
				thongTinCongTy.setMaCTTD(rs.getInt("MaCTTD"));
				thongTinCongTy.setTenCT(rs.getString("TenCT"));
				thongTinCongTy.setDiaChiCT(rs.getString("DiaChiCT"));
				thongTinCongTy.setSdtCT(rs.getString("SDTCT"));
				thongTinCongTy.setEmailCT(rs.getString("EmailCT"));
				thongTinCongTy.setMaTTTD(rs.getInt("MaTTTD"));
				thongTinCongTy.setNgayLapTTTD(rs.getDate("NgayLapTTTD"));
				thongTinCongTy.setTongSoLuongTD(rs.getInt("TongSoLuongTD"));
				thongTinCongTy.setNgayBatDauTD(rs.getDate("NgayBatDauTD"));
				thongTinCongTy.setNgayKetThucTD(rs.getDate("NgayKetThucTD"));
				
				boolean trangThaiDuyet = false;
				boolean trangThaiTTDD = false;
				if(rs.getInt("TrangThaiTTTD") == 1) {
					trangThaiTTDD = false;
				}
				
				thongTinCongTy.setTrangThaiTTTD(trangThaiTTDD);
				
				if(rs.getInt("TrangThaiDuyetTTTD") == 1) {
					trangThaiDuyet = true;
				}
				
				thongTinCongTy.setTrangThaiDuyetTTTD(trangThaiDuyet);				
				listThongTinCongTy.add(thongTinCongTy);
			}
			rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return listThongTinCongTy;
	}
	
	public int duyetThongTinTuyenDung(int maTTTD) {
		String query = "UPDATE `thongtintuyendung` SET `TrangThaiDuyetTTTD` = 1 WHERE `MaTTTD` = ?";

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = dataSource.getConnection();
			preparedStatement = connection.prepareStatement(query,
					Statement.RETURN_GENERATED_KEYS);
			preparedStatement.setInt(1, maTTTD);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		} finally {
			try {
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

}
